import { tbcurriculums } from '../../../atoms/db';


export type IData = Pick<ITbcurriculums, 'coursename' | 'week' | 'teachername' | 'classroom' | 'startteachingweek' | 'endteachingweek' | 'start_section' | 'end_section' | 'class' | 'course_id'>;

export default async function queryallcurriculums(class_id: string) {
	try {
		if (!class_id) {
			return [];
		}
		const all = await tbcurriculums()
			.select('*')
			.where({
				class_id
			});
		const mapdata = all.reduce((pre, cur) => {
			const arr = pre.get(cur.week) || [];
			arr.push(cur);
			return pre.set(cur.week, arr);
		}, new Map<number, ITbcurriculums[]>());
		function find(week: number, section: number) {
			const arr = mapdata.get(week);
			if (!arr) {
				return null;
			}
			// !!! 这里没有考虑一节课时内多门课程的问题.
			const d = arr.find((it) => {
				return section >= it.start_section && section <= it.end_section;
			});
			return d || null;
		}
		// 从第1节开始，查到第10节
		return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((section) => {
			// 周一
			const mon = find(1, section);
			// 周二
			const tue = find(2, section);
			// 周三
			const wen = find(3, section);
			// 周四
			const thu = find(4, section);
			// 周五
			const fri = find(5, section);
			// 周六
			const sat = find(6, section);
			// 周日
			const sun = find(7, section);
			return {
				section,
				mon,
				tue,
				wen,
				thu,
				fri,
				sat,
				sun
			};
		});
	} catch (error) {
		return [];
	}
}


